﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ivony.Html;
using Ivony.Html.Parser;

namespace WindowsFormsApp1 
{
	public partial class NBA卡片 : Form
	{
		public NBA卡片()
		{
			InitializeComponent();
		}

		private void 开始抓取_Click(object sender, EventArgs e)
		{



			try
			{

			
			for (int i = 0; i < 15120; i += 60)
			{
				var request = (HttpWebRequest)WebRequest.Create("https://www.sportsmemorabilia.com/search2.php?category=Autographed+NBA+Photos&No=" + i);
				var response = (HttpWebResponse)request.GetResponse();
				var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
				dateHtml(responseString, i);
			}
			}
			catch (Exception ec)
			{
				 
			}
		}

		private JumonyParser jp = new JumonyParser();
		public void dateHtml(string html, int index)
		{

			try
			{

			
			string savepath = @"D:\imgDate111";
			string c1 = "";
			var dataDocument = jp.Parse(html);
			var mumberlist = dataDocument.Find(".dir-product-ul").ToArray();
			for (int i = 0; i < mumberlist.Length; i++)
			{ 
				 
					 

					var alink = mumberlist[i].Find("a").ToArray();
					if (alink.Length > 0)
					{  
						c1 = alink[0].Attribute("title").Value();//获取名字  

					}

					var imguls = mumberlist[i].Find("img").ToArray();
					//获取但单张图片
					foreach (var imgli in imguls)
					{ 
						 
							var src = imgli.Attribute("src").Value(); 
							if (!string.IsNullOrWhiteSpace(c1))
							{
							int startsrc = src.LastIndexOf("-");
							string imgsrc = src.Substring(0, startsrc + 1);

							//DownloadImage(imgsrc+".jpg", savepath + "\\" + index + "\\", c1);
							DownloadImage(imgsrc + ".jpg", savepath + "\\"  + "\\", c1);
						} 
				}
			}
			}
			catch (Exception e)
			{

				throw;
			}
		}


		/// 网络地址
		/// 本地保存路径
		public static string DownloadImage(string url, string path, string name)
		{
			HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
			req.ServicePoint.Expect100Continue = false;
			req.Method = "GET";
			req.KeepAlive = true;
			req.ContentType = "image/*";
			HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
			System.IO.Stream stream = null;
			int i = url.LastIndexOf(".");
			string StrType = url.Substring(i);
			//string filename = Guid.NewGuid().ToString();
			try
			{
				CreateDirectory(path);
				// 以字符流的方式读取HTTP响应
				stream = rsp.GetResponseStream();
				Image.FromStream(stream).Save(path + "\\" + name.Replace(@"\", "").Replace(@"/", "") + StrType);
			}
			catch
			{

			}
			finally
			{
				// 释放资源
				if (stream != null) stream.Close();
				if (rsp != null) rsp.Close();
			}
			return name + StrType;
		}


		/// 网络地址
		/// 本地保存路径
		public static string DownloadImages(string url, string path, string name)
		{
			HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
			req.ServicePoint.Expect100Continue = false;
			req.Method = "GET";
			req.KeepAlive = true;
			req.ContentType = "image/*";
			HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
			System.IO.Stream stream = null;
			int i = url.LastIndexOf(".");
			string StrType = url.Substring(i);
			string filename = Guid.NewGuid().ToString();
			try
			{
				CreateDirectory(path);
				// 以字符流的方式读取HTTP响应
				stream = rsp.GetResponseStream();
				Image.FromStream(stream).Save(path + "\\" + name);
			}
			finally
			{
				// 释放资源
				if (stream != null) stream.Close();
				if (rsp != null) rsp.Close();
			}
			return filename + StrType;
		}


		#region 创建一个目录  
		/// <summary>  
		/// 创建一个目录  
		/// </summary>  
		/// <param name="directoryPath">目录的绝对路径</param>  
		public static void CreateDirectory(string directoryPath)
		{
			//如果目录不存在则创建该目录  
			if (!IsExistDirectory(directoryPath))
			{
				Directory.CreateDirectory(directoryPath);
			}
		}
		#endregion

		#region 检测指定目录是否存在  
		/// <summary>  
		/// 检测指定目录是否存在  
		/// </summary>  
		/// <param name="directoryPath">目录的绝对路径</param>          
		public static bool IsExistDirectory(string directoryPath)
		{
			return Directory.Exists(directoryPath);
		}
		#endregion

		private void button1_Click_1(object sender, EventArgs e)
		{
			for (int i = 0; i < 100000; i += 50)
			{
				int c = i;
			}

		}

	}
}
